package com.ewzj.bigdata.mvp.model;

import android.app.Application;

import com.ewzj.bigdata.app.utils.Apputils;
import com.ewzj.bigdata.mvp.model.api.service.CommonService;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseData;
import com.google.gson.Gson;
import com.jess.arms.integration.IRepositoryManager;
import com.jess.arms.mvp.BaseModel;

import com.jess.arms.di.scope.ActivityScope;

import javax.inject.Inject;

import com.ewzj.bigdata.mvp.contract.FeedBackContract;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Function;
import io.reactivex.functions.Predicate;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;


@ActivityScope
public class FeedBackModel extends BaseModel implements FeedBackContract.Model {
    private Gson mGson;
    private Application mApplication;
    private CommonService service;
    @Inject
    public FeedBackModel(IRepositoryManager repositoryManager, Gson gson, Application application) {
        super(repositoryManager);
        this.mGson = gson;
        this.mApplication = application;
        this.service=repositoryManager.obtainRetrofitService(CommonService.class);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mGson = null;
        this.mApplication = null;
        this.service=null;
    }

    @Override
    public Observable<BaseData<String>> feedBack(String content, List<String> path) {

        return Observable.fromIterable(path).map(new Function<String, File>() {
            @Override
            public File apply(String s) throws Exception {
                return new File(s);
            }
        }).filter(new Predicate<File>() {
            @Override
            public boolean test(File file) throws Exception {
                return file.exists();
            }
        }).toList().map(new Function<List<File>, List<MultipartBody.Part>>() {
            @Override
            public List<MultipartBody.Part> apply(List<File> files) throws Exception {
                List<MultipartBody.Part> fileparts=filesToMultipartBodyParts(files);
                fileparts.add(MultipartBody.Part.createFormData("content",content)) ;
                return fileparts;
            }
        }).flatMapObservable(new Function<List<MultipartBody.Part>, ObservableSource<BaseData<String>>>() {
            @Override
            public ObservableSource<BaseData<String>> apply(List<MultipartBody.Part> parts) throws Exception {
                return service.userFeedBack(parts);
            }
        });

    }
    private List<MultipartBody.Part> filesToMultipartBodyParts(List<File> files) {
        List<MultipartBody.Part> parts = new ArrayList<>(files.size());
        for (File file : files) {
            // TODO: 16-4-2  这里为了简单起见，没有判断file的类型
            RequestBody requestBody = RequestBody.create(MediaType.parse("image/png"), file);
            MultipartBody.Part part = MultipartBody.Part.createFormData("file", file.getName(), requestBody);
            parts.add(part);
        }

        return parts;
    }
}